<template>
  <div>

    <el-form ref="loginForm" :model="registerForm" :rules="rules" label-width="80px" class="login-box">
      <div class="login_face"><img src="../../assets/logo.png" style="width: 50px;height: 50px;" class="userAvatar"></div>

      <h3 class="login-title">欢迎使用后台管理系统</h3>
      <el-form-item label="账号" prop="userName">
        <el-input type="text" placeholder="请输入账号" v-model="registerForm.userName"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="registerForm.password"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" placeholder="请确认密码" v-model="registerForm.confirmPassword"/>
      </el-form-item>
      <el-form-item>
        <el-row>
          <el-col :span="17" :pull="8">
            <el-link type="primary" style="margin-left: 10px" href="#/login">已有账号？点击返回</el-link>

          </el-col>
          <el-col :span="7">
            <el-button type="primary" v-on:click="Register('loginForm')">注册</el-button>
          </el-col>
        </el-row>


      </el-form-item>

    </el-form>

  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {

        registerForm: {
          userName: '',
          password: '',
          confirmPassword:''
        },


        // 表单验证，需要在 el-form-item 元素中增加 prop 属性
        rules: {
          userName: [
            {required: true, message: '账号不可为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不可为空', trigger: 'blur'}
          ]
        },

        // 对话框显示和隐藏
        dialogVisible: false,

      }
    },
    methods: {
      Register(formName) {
        // 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.registerForm.password==this.registerForm.confirmPassword){
              this.$http.post("/register",this.registerForm).then(res=>{
                if(res.data.status){
                  this.$message({
                    message: res.data.message,
                    type: 'success'
                  });
                  this.$router.push("/login");
                }else{
                  this.$message.error(res.data.message);
                }
              })
            }else {
              this.$message.error("两次输入的密码不一致，请重新输入");
              this.registerForm.confirmPassword='';
              this.registerForm.password='';
            }

            // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
            // this.$router.push("/main");
            // this.$router.push("/login")
          } else {
            this.$message.error('请输入账号或密码！');
          }
        });
      },


    }
  }
</script>

<style scoped>

  .login-box {
    text-align: center;
    border: 1px solid #DCDFE6;
    width: 500px;
    margin: 180px auto;
    padding: 35px 35px 15px 35px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    box-shadow: 0 0 25px #909399;
  }

  .login-title {
    text-align: center;
    margin: 0 auto 40px auto;
    color: #303133;
  }

</style>
